#include "RDFSUtil.h"

Predicate triplePattern2Predicate(std::string tp[3]) {
	Term s(tp[0]);
	Term p(tp[1]);
	Term o(tp[2]);
	Predicate pred;
	if (!tp[1].compare("http://www.w3.org/2000/01/rdf-schema#subClassOf")) {
		pred.setName("subClass");
		pred.setArgument1(s);
		pred.setArgument2(o);
		pred.setNumOfArguments(2);
	} else if (!tp[1].compare(
			"http://www.w3.org/2000/01/rdf-schema#subPropertyOf")) {
		pred.setName("subProperty");
		pred.setArgument1(s);
		pred.setArgument2(o);
		pred.setNumOfArguments(2);
	} else if (!tp[1].compare("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) {
		pred.setName("type");
		pred.setArgument1(s);
		pred.setArgument2(o);
		pred.setNumOfArguments(2);
	} else {
		pred.setName("genTriple");
		pred.setArgument1(s);
		pred.setArgument2(p);
		pred.setArgument3(o);
		pred.setNumOfArguments(3);
	}
	return pred;
}

bool varCollision(std::list<std::string> pvars, std::list<std::string> rvars) {
	std::list<std::string>::iterator begin = pvars.begin();
	std::list<std::string>::iterator end = pvars.end();
	std::list<std::string>::iterator rvars_beg = rvars.begin();
	std::list<std::string>::iterator rvars_end = rvars.end();
	std::list<std::string>::iterator it;
	for (std::list<std::string>::iterator it3 = begin; it3 != end; it3++) {
		it = std::find(rvars_beg, rvars_end, *it3);
		if (it != rvars_end) {
			return true;
		}
	}
	return false;
}

std::string findCommonVariable(Predicate p1, Predicate p2){
	std::string var;
	if(p1.getVariablesNames().size()>p2.getVariablesNames().size()){
		std::list<std::string>::iterator i_beg=p1.getVariablesNames().begin();
		std::list<std::string>::iterator i_end=p1.getVariablesNames().end();
		std::list<std::string>::iterator p2_beg=p2.getVariablesNames().begin();
		std::list<std::string>::iterator p2_end=p2.getVariablesNames().end();
		std::list<std::string>::iterator it;
		for(std::list<std::string>::iterator i=i_beg;i!=i_end;i++){
			var=*i;
//			it = std::find(p2_beg,p2_end,*i);
//			if(it != p2_end){
//				return var;
//			}

			for(it=p2_beg;it!=p2_end;it++){
				if(!it->compare(*i))
					break;
			}
			if(it!=p2_end){	//证明找到, P2是待取代变元的尾巴子查询
				return var;
			}
		}
		std::cout << "NO COMMON VARS OR NO VARS at all in Predicate: " << p1.toString() << std::endl;
		var="NULL";
		return var;
	}else{
		std::list<std::string>::iterator i_beg=p2.getVariablesNames().begin();
		std::list<std::string>::iterator i_end=p2.getVariablesNames().end();
		std::list<std::string>::iterator p1_beg=p1.getVariablesNames().begin();
		std::list<std::string>::iterator p1_end=p1.getVariablesNames().end();
		std::list<std::string>::iterator it;
		for(std::list<std::string>::iterator i=i_beg;i!=i_end;i++){
			var=*i;
//			it = std::find(p1_beg,p1_end,*i);
//			if(it != p1_end){
//				return var;
//			}
			for(it=p1_beg;it!=p1_end;it++){
				if(!it->compare(*i))
					break;
			}
			if(it!=p1_end){	//证明找到, P2是待取代变元的尾巴子查询
				return var;
			}


		}
		std::cout << "NO COMMON VARS OR NO VARS at all in Predicate: " << p1.toString() << std::endl;
		var="NULL";
		return var;
	}
}

Binding* tuple2Substitution(NTuple t){
	std::vector<Binding> bs=t.getBindings();
	int bs_size=bs.size();

}
